package top.cwk.dubbo.core.service.hello;


import org.springframework.stereotype.Service;

import top.cwk.dubbo.api.hello.IHelloService;
import top.cwk.dubbo.entity.customer.Customer;
import top.cwk.dubbo.entity.customer.Goods;
import top.cwk.dubbo.entity.customer.ShoppingCart;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("helloService")
public class HelloServiceImpl implements IHelloService{

	@Override
	public String getHello() {
		return "Hello Springboot for dubbo!";
	}

	@Override
	public String sayHello(String hello) {
		System.out.println(hello);
		return "hello";
	}

	@Override
	public int getSq(int a) {
		return a*1000;
	}


	@Override
	public Customer getCustomerById(Integer customerId) {
		Customer customer = new Customer();
		customer.setCustomerid(customerId);
		customer.setUsername("test");
		customer.setPassword("123456");
		customer.setAddress("----");
		System.out.println(customer);
		return customer;
	}

	@Override
	public Customer saveCustomer(Customer customer) {
		Customer returnCus = new Customer();
		returnCus.setCustomerid(customer.getCustomerid());
		returnCus.setUsername(customer.getUsername());
		returnCus.setPassword(customer.getPassword());
		returnCus.setAddress(customer.getAddress());
		System.out.println(returnCus);
		return returnCus;
	}

	@Override
	public Double getPrice(Integer sum) {
		Double price = 9.85d;
		return sum*price;
	}

	@Override
	public Date getNowDate() {
		return new Date();
	}

	@Override
	public long getUuid() {
		return System.currentTimeMillis();
	}

	@Override
	public ShoppingCart showShoppingCart(int cartId) {
		List<Goods> list = new ArrayList<>();
		Goods goods = new Goods();
		goods.setId(100L);
		goods.setGoodsName("乐事薯片");
		goods.setGoodsDesc("乐事薯片--lesi");
		goods.setGoodsNum(2);
		goods.setGoodsAddress("beijing");
		goods.setGoodsPrice(4.8d);
		list.add(goods);
		Goods goods2 = new Goods();
		goods2.setId(101L);
		goods2.setGoodsName("苹果手机Iphone X");
		goods2.setGoodsDesc("苹果手机Iphone X 7999");
		goods2.setGoodsNum(1);
		goods2.setGoodsAddress("beijing");
		goods2.setGoodsPrice(7999d);
		list.add(goods2);
		ShoppingCart shoppingCart = new ShoppingCart();
		shoppingCart.setGoodsList(list);
		shoppingCart.setCratId(1L);
		shoppingCart.setDiscount(0.98);
		shoppingCart.setTotalPrice(list.parallelStream().mapToDouble(obj -> obj.getGoodsNum()*obj.getGoodsPrice()).sum());
		return shoppingCart;
	}

}
